package com.newyulong.implatform.service;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.newyulong.implatform.IMPApplication;
import com.newyulong.implatform.bean.UserInfoJsonBean;
import com.newyulong.implatform.util.CommonsUtil;
import com.newyulong.implatform.util.HttpUtil;
import com.newyulong.implatform.util.ParamUtil;

/**
 * 用户service
 * @author Eric
 *
 */
public class UserService {
	
	public UserService(Context context){
			
	}
	
	public String login(String userName ,String pas) throws ClientProtocolException, IOException{
		
		String method = "login.php";
		String requestUrl = CommonsUtil.getRequestUrl()+method;
		String openId = "";
		// 封装参数
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pwd", pas);
		paramMap.put("userid", userName);
		// 得到参数
		String paramStr = ParamUtil.getParamNormol(paramMap);
		String resultJson = "";
		try {
			// post请求
			resultJson = HttpUtil.requestByGet(requestUrl+"?"+paramStr);
			JSONObject json = new JSONObject(resultJson);
			String code = json.getString("code");
			if ("0".equals(code)) {
				// 登录成功,返回sessionkey
				openId = json.getString("openid");
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return openId;
	}
	
	/**
	 * 得到用户信息
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public UserInfoJsonBean getUserInfo() throws ClientProtocolException, IOException{
		UserInfoJsonBean bean = null;
		String method = "user_info.php";
		String requestUrl = CommonsUtil.getRequestUrl()+method;
		// 封装参数
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("sessionkey", IMPApplication.getInstance().getOpenId());
		// 得到参数
		String paramStr = ParamUtil.getParamNormol(paramMap);
		String resultJson = "";
		try {
			// post请求
			resultJson = HttpUtil.requestByGet(requestUrl+"?"+paramStr);
			//resultJson = HttpUtil.requestByPost(requestUrl, paramMap);
			JSONObject json = new JSONObject(resultJson);
			String code = json.getString("code");
			if ("0".equals(code)) {
				// 登录成功,返回sessionkey
				bean = new UserInfoJsonBean();
				String item = json.getString("item");
				JSONObject userInfoJson = new JSONObject(item);
				bean.setArea(userInfoJson.getString("area"));
				bean.setEmail(userInfoJson.getString("email"));
				bean.setEndTime(userInfoJson.getString("closingTime"));
				bean.setFlow(userInfoJson.getString("surplusFlow"));
				bean.setIntegral(userInfoJson.getString("consumeScores"));
				bean.setOver(userInfoJson.getString("closingTime"));
				bean.setUserId(userInfoJson.getString("userid"));
				bean.setUserName(userInfoJson.getString("uname"));
				bean.setUserType(userInfoJson.getString("mtype"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return bean;
	}
	
	
	
}
